package April._19.work8;

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        serialize2();

        unSerialize2();


    }

    private static void serialize2() throws IOException{
        // 创建序列化流对象
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("fileTest\\a.txt"));

        // 创建Teacher对象

        Address a1 = new Address("a", "a");
        Address a2 = new Address("b", "b");
        Address a3 = new Address("c", "c");

        User b1 = new User(1, "aaa", "1", a1);
        User b2 = new User(2, "bbb", "1", a2);
        User b3 = new User(3, "ccc", "1", a3);
        User[] users = new User[]{b1,b2,b3};

        out.writeObject(users);

        out.close();

    }

    private static void unSerialize2() throws IOException, ClassNotFoundException {
        // 创建反序列化流对象
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("fileTest\\a.txt"));
        // readObject
        User[] users = (User[]) in.readObject();

        for (User user : users) {
            System.out.println(user);
        }

        // close
        in.close();
    }
}
